<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index()
    {
        $this->display();
    }

    public function index1()
    {
        $this->display();
    }

    function send(){
        header("Access-Control-Allow-Origin: *");
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }

        $email = $_POST['email'];
        $username = $_POST['username'];
        $a = $this->rand_number(0,999999);
        $data['email'] = $email;
        $data['random'] = $a;
        $random = M('random');
        $title = 'NEWDAR 验证码' ;
        $message = "<html><body><div style=\"max-width: 500px; margin: 0 auto; background: #fff; box-shadow: #EBECED 0 0 0 1px\">

            <div style=\"box-shadow: rgba(8,25,30,0.085) 0 1px; text-align: center; padding: 15px 0; margin-bottom: 30px;\">
              <img src=\"http://dwt.artwe.com/default/images/201301/logo.png\" style=\" margin: 0 auto; display: block;\">
            </div>


          <div style=\"padding: 30px 60px\">
            <div style=\"font-size: 23px; font-weight: 600; margin-bottom: 15px;\">Hello  $username!</div>
                <div style=\"color: #8e98a7; line-height: 23px;\">
                    感谢注册NEWDAR。
                </div>
          </div>


          <div style=\"padding: 30px 60px\">
            <div style=\"box-shadow: #EBECED 0 0 0 1px; background: #FBFBFC; border-radius: 15px; padding: 30px 0; display: inline-block; width: 250px;text-align:center;\">
              <div style=\"text-transform: uppercase; font-size: 11px; font-weight: 600; color: #8f99a8;\">您的验证码</div>
              <div style=\"font-size: 30px; letter-spacing: 6px; font-weight: 600; margin-top: 15px;\">
                <span style=\"border-bottom: 1px dashed rgb(204, 204, 204); z-index: 1; position: static;\" t=\"7\" onclick=\"return false;\" data=\"229347\">$a</span>
              </div>
            </div>
          </div>


            <div style=\"box-shadow: rgba(8,25,30,0.085) 0 -1px; text-align: center; padding: 20px 0; margin-top: 30px; font-size: 20px;color:rgb(158, 158, 158)\"> newdarfy.com </div>

          </div></body></html>";


//        $random_list = $random->where("email = '$email'")->select();
        $demo = M('demo');
        $demo_list = $demo->where("email = '$email'")->select();
        if($demo_list){
            $this->ajaxReturn(array(
                'info' => '-3'               //邮箱已经注册
            ));
        }

        $result = $random->add($data);
        if(!$result){
            $this->ajaxReturn(array(
                'info' => '-1'  //验证码生成失败
            ));
        }else{
            $mail_result = $this->SendMail($email,$title,$message);
            if(!$mail_result){
                $this->ajaxReturn(array(
                    'info' => '-2'  //邮件发送失败
                ));
            }else{
                $this->ajaxReturn(array(
                    'info' => '1'  //邮件发送成功，跳转到验证码
                ));
            }
        }
        $this->ajaxReturn($data,'json');

    }


    function register(){
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }
        $demo = M('demo');
        $code = $_POST['code'];
        $email = $_POST['email'];
        $data['name'] = $_POST['name'];
        $data['country'] = $_POST['country'];
        $data['email'] = $_POST['email'];
        $data['tel'] = $_POST['tel'];
        $data['password'] = $_POST['password'];
        $data['ip'] = get_client_ip();
        $data['language'] = $_POST['language'];
        $random = M('random');
        $random_list = $random->where("email= '$email'")->order('created_at desc')->limit(1)->select();
        if($random_list){
            if($random_list['0']['random'] == $code){
                $demo_list = $demo->add($data);
                if($demo_list){
                    $this->ajaxReturn(array(
                        'info' => '1'                             //创建成功
                    ));
                }else{
                    $random->where("email= '$email'")->delete();
                    $this->ajaxReturn(array(
                        'info' => '-2'                             //创建失败
                    ));
                }
            }else{
                $random->where("email= '$email'")->delete();
                $this->ajaxReturn(array(
                    'info' => '-1'                             //验证失败
                ));
            }
        }else{
            $random->where("email= '$email'")->delete();
            $this->ajaxReturn(array(
                'info' => '-1'                             //验证失败
            ));
        }
    }

    function  SendMail($address,$title,$message)
    {
        import('ORG.Net.PHPMailer');
//        import('@ORG.PHPMailer') ;
        vendor("PHPMailer.class#phpmailer");
        vendor("PHPMailer.class#smtp");
        $mail = new \PHPMailer();
        // 设置PHPMailer使用SMTP服务器发送Email
        $mail->IsSMTP();
        // 设置邮件的字符编码，若不指定，则为'UTF-8'
        $mail->CharSet = 'UTF-8';
        // 添加收件人地址，可以多次使用来添加多个收件人
        $mail->AddAddress($address);
        // 设置邮件正文
        $mail->MsgHTML($message);
        $mail->Body = $message;
        // 设置邮件头的From字段。
        $mail->From = C('MAIL_ADDRESS');
        // 设置发件人名字
        $mail->FromName = '小明';
        // 设置邮件标题
        $mail->Subject = $title;
        // 设置SMTP服务器。
        $mail->Host = C('MAIL_SMTP');
        // 设置为“需要验证”
        $mail->SMTPAuth = true;
        // 设置用户名和密码。
        $mail->Username = C('MAIL_LOGINNAME');
        $mail->Password = C('MAIL_PASSWORD');
        // 发送邮件。
        return ($mail->Send());

    }

    function rand_number ($min, $max) {
        return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
    }
    //404
}
